<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 重构字符串 -->
    <script>
      var reorganizeString = function (S) {
        var h = new Map(),
          r = [],
          odd = -1,
          even = -2,
          m = S.length >> 1,
          b = (S.length + 1) >> 1
        for (var i = 0; i < S.length; i++) h.set(S[i], h.has(S[i]) ? h.get(S[i]) + 1 : 1)
        h.forEach((n, s, _, i = n) => {
          if (n > b) (r = []), h.clear()
          else while (i--) r[n <= m && odd < S.length - 2 ? (odd += 2) : (even += 2)] = s
        })
        return r.join('')
      }
      console.log(reorganizeString('aab'))
    </script>
  </body>
</html>
